/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$        : main.c                                        	      *
 *  $Author$           : ChipON AE/FAE Group                                  *
 *  $Data$             : 2022-3-17                                           *
 *  $Software Version  : V1.5	                                              *
 *  $Description$     : This is a KF32A156 device StdPeriph Template		  *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyrght protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 ******************************************************************************

/******************************************************************************
*                        		Include Files                                 *
******************************************************************************/
#include "system_init.h"
#include "Canfd.h"
#include "Usart.h"
static void Led_Gpio_Init(void);
static void Led_Flip(void);
uint32_t Receive_Flag = 0x00;
static void Led_Gpio_Init(void)
{
	/* PC6/PC5/PC2*/

	GPIO_Write_Mode_Bits(GPIOC_SFR, GPIO_PIN_MASK_6|GPIO_PIN_MASK_5|GPIO_PIN_MASK_2, GPIO_MODE_OUT);

	GPIO_Set_Output_Data_Bits(GPIOC_SFR,GPIO_PIN_MASK_6|GPIO_PIN_MASK_5|GPIO_PIN_MASK_2,Bit_SET);
}


/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
static void Led_Flip(void)
{
	GPIO_Toggle_Output_Data_Config(GPIOC_SFR, GPIO_PIN_MASK_6|GPIO_PIN_MASK_5|GPIO_PIN_MASK_2);

}


/*
 * 500K/2M  BAUDRATE
 * */
int main()
{
	uint8_t i=0;
	/* Initialize the system clock */
	SystemInit(SYSCLK_FREQ);
	/* Setup SysTick Timer as delay function */
	systick_delay_init(SYSCLK_FREQ);
	Canfd_Gpio_Init();
	Led_Gpio_Init();
	Can_m_FdControllerDeInit(CANfd7);
	Can_m_FdControllerInit(CANfd7, Canfd_Controller_AllConfig,Initindex_0);

	/* Initialize Receveive Mailbox */
	Can_m_FdMailBox_Write(CANfd7,Mailbox_0,&Receive_Canfd_MailboxHeader[0]);
	Can_m_FdMailBox_Write(CANfd7,Mailbox_1,&Receive_Canfd_MailboxHeader[0]);
	Usart_Gpio_Init();
//	/*
//	 * Set Usart0 working mode :
//	 *    - Set Usart0 to async mode
//	 *    - Set baudrate to 115200
//	 *    - Set Usart to send enable
//	 */
	USART_Mode_Config(USART1_SFR);
	fprintf(USART1_STREAM,"power on!\r\n");
	while (1)
	{
		/* Initialize Transmit Mailbox */
		if(Can_m_FdMailBox_Write(CANfd7,Mailbox_2,&Transmit_Canfd_MailboxHeader[0]) == CAN_OK)
		{
			/* Enable Transmit */
			(void)Can_m_FdTransmit(CANfd7);
		}
		else
		{
			fprintf(USART1_STREAM,"can send erro!\r\n");
		}
		if(Receive_Flag > 0x00)
		{

			Receive_Canfd_MailboxHeader[0].TransceiveType = MAIL_TRANSMIT;
			if(Can_m_FdMailBox_Write(CANfd7,Mailbox_3,&Receive_Canfd_MailboxHeader[0]) == CAN_OK)
			{
				/* Enable Transmit */
				(void)Can_m_FdTransmit(CANfd7);
				Receive_Flag--;
			}
		}
		systick_delay_ms(500);
		Led_Flip();
	}
}



/**
 *  @brief : Reports the name of the source file and the source line number
 *           where the assert_param error has occurred.
 *  @param in :	file pointer to the source file name
 * 	@param in :	line assert_param error line source number
 *  @param out :None
 *  @retval :None
 */
void check_failed(uint8_t *File, uint32_t Line)
{
	/* User can add his own implementation to report the file name and line number,
		ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
		;
	}
};


